package com.tc.travel.submitter.service;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tc.travel.util.HttpClientUtil;
import com.tc.travel.util.Json;
import com.tc.travel.util.PropertyUtil;
import com.tc.travel.util.exception.ComRuntimeException;

@Component
public class SessionBo {
	public String getCookie() {
		String url = PropertyUtil.interfaceUrlGetCookie;
		Map<String, String> params = new HashMap<String, String>();
		Map<String, String> headers = new HashMap<String, String>();

		String html = HttpClientUtil.doGet(url, params, headers);
		Json<String> json = toJsonString(html);
		if (json.isSuccess()) {
			return json.getObj();
		} else {
			throw new ComRuntimeException("getCookie error: " + json.getMsg());
		}
	}

	public static Json<String> toJsonString(String jsonStr) {
		Type jsonType = new TypeToken<Json<String>>() {
		}.getType();
		Json<String> json = new Gson().fromJson(jsonStr, jsonType);
		return json;
	}
}
